package com.cheetah.start.system.common.controller;

import com.cheetah.start.common.annotation.ApiCount;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultData;
import com.cheetah.start.common.config.property.UpLoadProprety;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.FileStatusEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.common.utils.MyFileUtil;
import com.cheetah.start.system.common.async.FileAsync;
import com.cheetah.start.system.file.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * 通用
 *
 * @author xjhmwj
 * @date 2021/6/11 17:24
 */
@Api(tags = "通用上传")
@Slf4j
@ApiCount
@CrossOrigin
@RestController
@RequestMapping("/common")
public class CommonController extends ResultController {


    @Autowired
    private UpLoadProprety upLoadProprety;
    @Resource
    private FileAsync sysFileAsync;
    @Value("${server.servlet.context-path}")
    private String suffix;


    /**
     * 通用上传
     *
     * @param file 文件
     * @return {@link Result}
     */
    @Log(title = "上传", describe = "上传", type = BusinessTypeEnum.ADD, logType = LogTypeEnum.OPERATE)
    @ApiOperation(value = "上传")
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        // 检查后缀
        MyFileUtil.checkCommonSuffix(file);
        // 原文件名
        String originalFilename = file.getOriginalFilename();
        // 生成文件名
        String dateFileName = MyFileUtil.getDateFileName(file);
        // 本地路径
        String fullPath = upLoadProprety.getPath() + dateFileName;
        File fileHave = MyFileUtil.getFileAndCheck(fullPath);
        // 文件上传
        file.transferTo(fileHave);
        long size = file.getSize();
        String hostFullPath = MyFileUtil.getHostFullName(suffix, upLoadProprety.getProfileHost(), dateFileName);
        String hostHalfPath = MyFileUtil.getHostHalfPath(suffix, dateFileName);
        ResultData res = new ResultData();
        res.put("full", hostFullPath)
                .put("half", hostHalfPath)
                .put("size", size / 1000)
                .put("name", originalFilename);
        SysFile sysFile = new SysFile();
        sysFile.setFileSize((int) (size / 1000)).setFileName(originalFilename)
                .setFilePath(hostFullPath).setFileNowName(fileHave.getName())
                .setFileUrl(hostHalfPath).setFileRealPath(fullPath)
                .setCreateTime(new Date())
                .setFileStatus(FileStatusEnum.USE.getCode());
        sysFileAsync.record(sysFile);
        return success(res);
    }


}
